<?php

class Db {

    public $result = "";
    public $db;

    function Db() {
        $this->db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
        if ($this->db->connect_errno) {
            echo "db 접속실패: (" . $this->db->connect_errno . ") " . $this->db->connect_error;
        }
        $this->db->query('SET NAMES utf8');
        $this->db->query("set session character_set_connection=utf8;");
        $this->db->query("set session character_set_results=utf8;");
        $this->db->query("set session character_set_client=utf8;");
    }

    function select($table, $where, $field = "*") {

        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        return $result;
    }

    function insert($table, $data) {
        $sql = "insert into $table  $data ";
        if ($this->get_result($sql)) {
            return true;
        } else {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
    }

    function insert_array($table, $arr) {
        if (!is_array($arr)) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> 인자로 배열을 넣어야 합니다.");
        }
        $fields = "";
        $vals = "";
        $dot = ",";
        $i = 1;
        foreach ($arr as $field => $val) {
            if ($i == count($arr)) {
                $dot = "";
            }
            $fields .= $field . $dot;
            $vals.="'" . $val . "'" . $dot;
            $i++;
        }
        $data = "(" . $fields . ") values (" . $vals . ")";



        $sql = "insert into $table  $data ";
        if ($this->get_result($sql)) {
            return true;
        } else {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
    }

    function update($table, $where, $data) {
        $sql = "update $table set $data $where";
        if ($this->get_result($sql)) {
            return true;
        } else {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
    }

    function delete($table, $data) {
        $sql = "delete from $table $data";
        if ($this->get_result($sql)) {
            return true;
        } else {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
    }

    function drop($table) {
        $sql = "drop table $table";
        if ($this->get_result($sql)) {
            return true;
        } else {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
    }

    function create($table) {
        $sql = "create table $table";
        if ($this->get_result($sql)) {
            return true;
        } else {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
    }

    function get_rows($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_row();
        return $result;
    }

    function get_sum_of_value($table, $where, $field = "*") {
        $sql = "select sum($field) from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_row();
        if ($result[0]) {
            return $result[0];
        } else {
            return 0;
        }
    }

    function get_count($table, $where, $count_target = "uid") {
        $sql = "select count($count_target) from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_row();
        if ($result[0]) {
            return $result[0];
        } else {
            return;
        }
    }

    function get_result($sql) {
        $sql = trim($sql);
        $result = $this->db->query($sql);
        return $result;
    }

//결과를 보여준다
    function show_result_by_object($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $this->result = $result;
        $this->set_style();
    }

    function show_result_by_array($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $this->result = $result;
        $this->set_style();
    }

    //값을 반환받는다
    function return_result_by_object($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_object();
        $this->result = $result;
        return $this->result;
    }

    function return_result_by_array($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_array(MYSQLI_BOTH);
        $this->result = $result;
        return $this->result;
    }

    //값을 반환받는다
    function get_object($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_object();
        $this->result = $result;
        return $this->result;
    }

    function get_array($table, $where, $field = "*") {
        $sql = "select $field from $table $where";
        $result = $this->get_result($sql);
        if (!$result) {
            throw new Exception("<br>method : " . __METHOD__ . "<br>query  :  " . $sql);
        }
        $result = $result->fetch_array(MYSQLI_BOTH);
        $this->result = $result;
        return $this->result;
    }

    function set_style() {
        echo"warnning : Db 클래스를 상속받아 'set_style()'함수를 오버라이딩해서 사용하시오!<br>";
    }

    function set_style_view() {
        echo"warnning : Db 클래스를 상속받아 'set_style()'함수를 오버라이딩해서 사용하시오!<br>";
    }

    function close() {
        $this->db->close();
    }

}
